﻿using LiveChartsCore;
using LiveChartsCore.Drawing;
using LiveChartsCore.Drawing.Layouts;
using LiveChartsCore.Kernel;
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView.Drawing.Layouts;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.SKCharts;
using SkiaSharp;

namespace FileTransferDesktop.Views.Controls
{
    public class SpeedChartTooltip : SKDefaultTooltip
    {
        protected override void Initialize(Chart chart)
        {
            Wedge = 5;

            Geometry.Fill = new SolidColorPaint(new SKColor(255, 255, 255, 180)); // 半透明灰色
            Geometry.Stroke = new SolidColorPaint(new SKColor(100, 100, 100, 200));
            Geometry.Wedge = Wedge;
            Geometry.WedgeThickness = 2;

            this.Animate(
                new Animation(EasingFunctions.QuadraticOut, TimeSpan.FromMilliseconds(300))
            );
        }

        protected override Layout<SkiaSharpDrawingContext> GetLayout(
            IEnumerable<ChartPoint> foundPoints,
            Chart chart
        )
        {
            var layout = new StackLayout
            {
                Padding = new Padding(6),
                Orientation = ContainerOrientation.Vertical,
                HorizontalAlignment = Align.Middle,
                VerticalAlignment = Align.Middle,
            };

            foreach (var point in foundPoints)
            {
                var series = point.Context.Series;
                var value = point.Coordinate.PrimaryValue;
                var isMb = value >= 1024;
                var unit = isMb ? "MB/s" : "KB/s";
                var displayValue = isMb ? value / 1024.0 : value;

                // 获取图例图形
                var miniature =
                    (IDrawnElement<SkiaSharpDrawingContext>)series.GetMiniatureGeometry(point);

                var label = new LabelGeometry
                {
                    Text = $"{series.Name}: {displayValue:F2} {unit}",
                    Paint = new SolidColorPaint(new SKColor(30, 30, 30)),
                    TextSize = 12,
                    Padding = new Padding(4, 0, 0, 0),
                    VerticalAlign = Align.Start,
                    HorizontalAlign = Align.Start,
                };

                var row = new StackLayout
                {
                    Orientation = ContainerOrientation.Horizontal,
                    Padding = new Padding(0, 2),
                    VerticalAlignment = Align.Start,
                    HorizontalAlignment = Align.Start,
                    Children = { miniature, label },
                };

                layout.Children.Add(row);
            }

            return layout;
        }

        public override void Show(IEnumerable<ChartPoint> foundPoints, Chart chart)
        {
            base.Show(foundPoints, chart);
        }

        public override void Hide(Chart chart)
        {
            base.Hide(chart);
        }
    }
}
